package com.bdqn.tour.dao;



import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class uerDao {
    static String Driver;
    static String user;
    static String pwd;
    static String url;

    /**
     * 内信息被加载时执行一次
     */
    static {
        //读取db。properties文件
        Properties properties = new Properties();
        try {
            //创建一个读取配置文件的输入流
            InputStream is = uerDao.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(is);//开始读取文件李的信息到properties对象中
            Driver = properties.getProperty("jdbc.driver");
            user=properties.getProperty("jdbc.user");
            pwd=properties.getProperty("jdbc.password");
            url=properties.getProperty("jdbc.url");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getCollection() {
        Connection connection = null;
        try {
            Class.forName(Driver);
            connection= DriverManager.getConnection(url,user,pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.fillInStackTrace();
        }
        return connection;
    }
    public void closeAll(Connection connection, PreparedStatement P, ResultSet resultSet){
        try {
            if (resultSet!=null){
                resultSet.close();
            }
            if (P!=null){
                P.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.fillInStackTrace();
        }
    }
    public int execute(String sql,Object...param){
        Connection connection=this.getCollection();
        int hang = 0;
        try {
            PreparedStatement statement=connection.prepareStatement(sql);
            for (int i=0;i<param.length;i++){
                statement.setObject(i+1,param[i]);
            }
            hang=statement.executeUpdate();
            this.closeAll(connection,statement,null);
        } catch (SQLException e) {
            System.out.println("执行sql出错");
            e.fillInStackTrace();
        }
        return hang;
    }
}
